<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//relations
//	type
//	1		relative
//	2		bundle
//	3		promotion

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class ProductRelationBase extends BaseItemBase {
	//target ==> products_id
	var $_target = 0;
	//
	var $_type = 1;
	
	//constructor
	function ProductRelationBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_products_relations';
		$this->prefix = 'relations_';
		
		$this->filter_fields = array('target:=');
		$this->filter_map = array('target' => 'target:=');
		$this->sort_fields = array('target+', 'sort_order+', 'id+');
	}
	
	//setter/getter
	function setTargetType($target = 0, $type = 0) {
		if( $target > 0 ) {
			$this->_target = $target;
		}
		if( $type > 0 ) {
			$this->_type = $type;
		}
	}
	
	//methods
	function checkTargetItem($target, $value, $type = 0) {
		if( $type == 0 ) {
			$type = $this->_type;
		}
		$query = "select count(*) as count from " . $this->table . " where " . $this->_pf('target') . " = '" . (int)$target . "' " 
				. " and " . $this->_pf('value') . " = '" . (int)$value . "' " 
				. " and " . $this->_pf('type') . " = '" . (int)$type . "'";
		
		return $this->adb->checkCount($query);
	}
	
	function getItemsDetail($filter = '', $check = 0, $limit = true) {
		$items = $this->getItems($filter, $check, $limit);
		$ids = $this->getListKey($items, 'value');
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/product/product.class.php');
		$mObj = new ProductBase();
		$mObj->format = true;
		
		$ps = $mObj->getItems(array('ids' => $ids), -1, false);
		$ps = $this->setListKey($ps, 'id');
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$items[$i]['item'] = $ps[$items[$i]['value']];
		}
		
		return $items;
	}
	
	function onAddItemFilterBind($data_array) {
		$fb = array();
		if( $this->_target > 0 ) {
			$fb['_target'] = (int)$this->_target;
		}
		if( $this->_type > 0 ) {
			$fb['_type'] = (int)$this->_type;
		}
		
		return $fb;
	}
	
	function onUpdateItemFilterBind($items_id, $data_array) {
		$fb = array();
		if( $this->_target > 0 ) {
			$fb['_target'] = (int)$this->_target;
		}
		if( $this->_type > 0 ) {
			$fb['_type'] = (int)$this->_type;
		}
		
		return $fb;
	}
	
	//
	function deleteTargetItems($target_id, $items_ids = '') {
		$ids = $this->getItemsIDs(array('target' => $target_id), -1, false);
		if( is_array($items_ids) ) {
			$ids = array_values(array_intersect($items_ids, $ids));
		}
		
		return $this->deleteItems($ids);
	}
	
	//private
	function _get_query_str_custom() {
		$query_str = "";
		if( $this->_target > 0 ) {
			$query_str .= " and " . $this->_pf('target') . " = '" . (int)$this->_target . "'";
		}
		if( $this->_type > 0 ) {
			$query_str .= " and " . $this->_pf('type') . " = '" . (int)$this->_type . "'";
		}
		
		return $query_str;
	}
}
//
?>